package com.crysta1.picviewer;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import androidx.viewpager2.adapter.FragmentStateAdapter;

import java.util.List;

public class ImagePagerAdapter extends FragmentStateAdapter {

    private final List<ImageItem> imageItems;

    public ImagePagerAdapter(
            @NonNull FragmentActivity fragmentActivity,
            @NonNull List<ImageItem> imageItems
    ) {
        super(fragmentActivity);
        this.imageItems = imageItems;
    }

    @NonNull
    @Override
    public Fragment createFragment(int position) {
        // 关键点：通过工厂方法传递完整参数
        return DetailFragment.newInstance(
                imageItems.get(position).url
        );
    }

    @Override
    public int getItemCount() {
        return imageItems.size();
    }

}